package com.woke.method;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class Bitmapr {
	

	public Bitmap toRoundBitmap(Bitmap bitmap, int width2) {  
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();  

        int r = 0;  
        r = width2;

//        if(width > height) {  
//            r = height;  
//        } else {  
//            r = width;  
//        }  

//        Bitmap backgroundBmp = Bitmap.createBitmap(width,  
//                 height, Config.ARGB_8888);  //
        Bitmap backgroundBmp = Bitmap.createBitmap(r,  
        		r, Config.ARGB_8888);  //������ͼƬ�Ŀ������

        Canvas canvas = new Canvas(backgroundBmp);  
        Paint paint = new Paint();  

        paint.setAntiAlias(true);  

        RectF rect = new RectF(0, 0, r, r); //
        canvas.drawRoundRect(rect, r/2, r/2, paint);  
//        canvas.drawRoundRect(rect, r, r, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, null, rect, paint);
        return backgroundBmp;  
    }  
	
	/**

	   */
	  public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength){
	   if(null == bitmap || edgeLength <= 0)
	   {
	    return  null;
	   }
	                                                                                 
	   Bitmap result = bitmap;
	   int widthOrg = bitmap.getWidth();
	   int heightOrg = bitmap.getHeight();
	                                                                                 
	   if(widthOrg > edgeLength && heightOrg > edgeLength)
	   {
	    //ѹ����һ����С������edgeLength��bitmap
	    int longerEdge = (int)(edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg));
	    int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
	    int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
	    Bitmap scaledBitmap;
	                                                                                  
	          try{
	           scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
	          }
	          catch(Exception e){
	           return null;
	          }
	                                                                                       
	       //��ͼ�н�ȡ���м�������β��֡�
	       int xTopLeft = (scaledWidth - edgeLength) / 2;
	       int yTopLeft = (scaledHeight - edgeLength) / 2;
	                                                                                     
	       try{
	        result = Bitmap.createBitmap(scaledBitmap, xTopLeft, yTopLeft, edgeLength, edgeLength);
	        scaledBitmap.recycle();
	       }
	       catch(Exception e){
	        return null;
	       }       
	   }
	                                                                                      
	   return result;
	  }
	
}
